using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.DonVi.Library.Providers;
using NCS.Module.DonVi.Library.DonVi;
using NCS.Library;
using NCS.Library.Utilities;

namespace NCS.Module.DonVi.Library.Controler
{
    public class DonViControler
    {
        /// <summary>
        /// Get all data with TrangThai = 1
        /// </summary>
        /// <returns></returns>
        public static DataTable GetDonViAvailable()
        {
            return DataAccessProvider.instance().GetDonViAvailable();
        }

        /// <summary>
        /// Get all data
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllDonVi()
        {
            return DataAccessProvider.instance().GetAllDonVi();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iStatus"></param>
        /// <returns>all record that had TrangThai=1</returns>
        public static DataTable GetAllDonViFilter(int iStatus)
        {
            return DataAccessProvider.instance().GetAllDonViFilter(iStatus);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>return one record</returns>
        public static DonViInfo GetInfo(int id)
        {
            return DataAccessProvider.instance().GetInfo(id);
        }

        /// <summary>
        /// Set index of record
        /// </summary>
        /// <param name="id"></param>
        /// <param name="index"></param>
        /// <returns>return number of record changes</returns>
        public static int SetIndex(int id, int index)
        {
            return DataAccessProvider.instance().SetIndex(id, index);
        }

        /// <summary>
        /// Set status 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns>return number of records changes</returns>
        public static int SetStatus(int id, bool status)
        {
            return DataAccessProvider.instance().SetStatus(id, status);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objDonViInfo"></param>
        /// <returns>return number of record changes</returns>
        public static int CreateDonVi(DonViInfo objDonViInfo)
        {
            return DataAccessProvider.instance().CreateDonVi(objDonViInfo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objDonViInfo"></param>
        /// <returns>return number of record changes</returns>
        public static int UpdateDonVi(DonViInfo objDonViInfo)
        {
            return DataAccessProvider.instance().UpdateDonVi(objDonViInfo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objDonViInfo"></param>
        /// <returns>return number of record changes</returns>
        public static int DeleteDonVi(int id)
        {
            return DataAccessProvider.instance().DeleteDonVi(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>return quantity records</returns>
        public static int GetCount()
        {
            return DataAccessProvider.instance().GetCount();
        }

        public static void LoadDonVi(ListItemCollection lstItems, int portalid)
        {
            DataTable dtTable = null;
            dtTable = GetDonVi(0, portalid);
            foreach (DataRow row in dtTable.Rows)
            {
                ListItem item = new ListItem(row["TenDonVi"].ToString(), row["DonViID"].ToString());
                item.Attributes.Add("Level", "0");
                lstItems.Add(item);
                LoadForCurItem(lstItems, item, portalid);
            }
        }

        public static DataTable GetDonVi(int parentId, int portalid)
        {
            DataTable dtTable = null;
            dtTable = DataAccessProvider.instance().GetDonVi(parentId, portalid, AppEnv.GetLanguage());
            return dtTable;
        }

        private static void LoadForCurItem(ListItemCollection lstItems, ListItem curItem, int portalid)
        {
            int curId = ConvertUtility.ToInt32(curItem.Value);
            int level = ConvertUtility.ToInt32(curItem.Attributes["Level"]);
            level++;
            DataTable dtChild = GetDonVi(curId, portalid);
            foreach (DataRow row in dtChild.Rows)
            {
                ListItem item = new ListItem(MiscUtility.StringIndent(level) + row["TenDonVi"].ToString(), row["DonViID"].ToString());
                item.Attributes.Add("Level", level.ToString());
                lstItems.Add(item);
                LoadForCurItem(lstItems, item, portalid);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="lang"></param>
        /// <returns>return all child with id</returns>
        public static DataTable GetAllChildByID(int id, string lang)
        {
            return DataAccessProvider.instance().GetAllChildByID(id, lang);
        }
    }
}
